<template>
  <div class="container">
    <!-- <header>
      <span @click="back">
        <i class="iconfont icon-jiantou1"></i>
        <span>返回</span>
      </span>
    </header> -->
    <Header>
      修改地址
    </Header>
    <van-address-edit
      :area-list="areaList"
      :address-info="addressInfo"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="select"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>
<script>
import Vue from 'vue'
import { AddressEdit, Toast, Area } from 'vant'
import Header from '@/components/header.vue'
import areaList from '@/area/area.js'
import axios from '@/utils/request'
Vue.use(AddressEdit)
Vue.use(Area)
export default {
  data () {
    return {
      areaList,
      searchResult: [],
      select: ['请选择', '请选择', '请选择']
    }
  },
  computed: {
    addressInfo () {
      if (JSON.parse(localStorage.getItem('edit'))) {
        return JSON.parse(localStorage.getItem('edit'))
      } else {
        return {}
      }
    }
  },
  components: {
    Header
  },
  methods: {
    onSave (content) {
      const address = JSON.parse(localStorage.getItem('edit')) || ''
      const userid = localStorage.getItem('userid')
      axios.post('/users/addsite', {
        addressDetail: content.addressDetail,
        areaCode: content.areaCode,
        city: content.city,
        country: content.country,
        county: content.county,
        isDefault: content.isDefault,
        name: content.name,
        postalCode: content.postalCode,
        province: content.province,
        tel: content.tel,
        userid,
        addressid: address.addressid
      }).then(res => {
        if (res.data.code !== '200') {
          this.$router.push('/login')
        } else {
          Toast('保存成功')
          this.$router.back()
          localStorage.removeItem('edit')
        }
      })
    },
    onDelete () {
      const address = JSON.parse(localStorage.getItem('edit')) || ''
      const userid = localStorage.getItem('userid')
      axios.post('/users/deleteaddress', { addressid: address.addressid, userid }).then(res => {
        if (res.data.code !== '200') {
          this.$router.push('/login')
        } else {
          localStorage.removeItem('edit')
          Toast('删除成功')
          this.$router.back()
        }
      })
    },
    onChangeDetail (val) {
    }
  }
}
</script>
<style lang="scss">
@import "@/lib/reset.scss";
.container {
  header {
    @include flexbox();
    @include justify-content(space-between);
  }
}
</style>
